﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.Server.Interface;
using Swork03_28.Domain.RBAC;

namespace RBAC.Poject.Controllers
{
    /// <summary>
    /// 菜单控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class MenusController : ControllerBase
    {
        private readonly IMenuServer _server;

        /// <summary>
        /// 构造方式注入
        /// </summary>
        /// <param name="server"></param>
        public MenusController(IMenuServer server)
        {
            _server = server;
        }


        /// <summary>
        /// 添加菜单信息
        /// </summary>
        /// <param name="menuInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> MenuAdd(MenuInfo menuInfo)
        {
            return await _server.MenuAdd(menuInfo);
        }

        /// <summary>
        /// 获取菜单名称
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<MenuItem> GetMenuItems(int pid)
        {
            return _server.GetMenuItems(pid);
        }

        /// <summary>
        /// 获取菜单信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetMenuInfos(int pageIndex, int pageSize)
        {
            var data = await _server.GetMenuInfos(pageIndex, pageSize);
            return Ok(new
            {
                data.totalCount,
                data.pageCount,
                data.list
            });
        }

        /// <summary>
        /// 逻辑删除菜单信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut]
        public Task<object> Del(int id)
        {
            return _server.Del(id);
        }

        /// <summary>
        /// 逻辑删除多个菜单
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> DelMuch(List<int>? ids)
        {
            return await _server.DelMuch(ids);
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MenuInfo> GetMenu(int id)
        {
            return await _server.GetMenu(id);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> Update(MenuInfo info)
        {
            return await _server.Update(info);
        }

        /// <summary>
        /// 获取菜单树型结构
        /// </summary>
        /// <param name="pId">父编号</param>
        /// <returns>树型菜单</returns>
        [HttpGet]
        public List<MenuItem> GetMenuTree(int pId, string? name)
        {
            return _server.GetMenuTree(pId, name);
        }

        /// <summary>
        /// 获取角色树型结构
        /// </summary>
        /// <returns>树型菜单</returns>
        [HttpGet]
        public async Task<List<MenuItem>> GetRoleTree(string? name)
        {
            return await _server.GetRoleTree(name);
        }

        /// <summary>
        /// 获取角色的权限id
        /// </summary>
        /// <param name="Rid">角色编号</param>
        /// <returns>权限id</returns>
        [HttpGet]
        public async Task<List<int>> MenuTreeIds(int Rid)
        {
            return await _server.MenuTreeIds(Rid);
        }

        /// <summary>
        /// 给角色分配权限
        /// </summary>
        /// <param name="Rid">角色编号</param>
        /// <param name="MenuId">权限编号</param>
        /// <returns>受影响的行数</returns>
        [HttpPost]
        public async Task<int> RoleMenuAdd(int Rid, List<int> MenuId)
        {
            return await _server.RoleMenuAdd(Rid, MenuId);
        }
    }
}
